using System;
using System.Globalization;
using SF.Code.Extension.HttpHelper.Downloader;

namespace SG.Ling.AutoTranslator.Translation
{
    public class TranslationTuple : Tuple<TranslationKey, TranslationRecord>, IPersistableWebResult<TranslationKey, TranslationRecord>
    {
        private static CultureInfo IntToCultureInfo(int cultureInfo)
        {
            return cultureInfo == 0 ? null : CultureInfo.GetCultureInfo(cultureInfo);
        }

        public TranslationTuple(ref TranslationKey key, ref TranslationRecord record)
            : base(key, record)
        {
        }

        public TranslationTuple(CultureInfo sourceLanguage, string sourceText, string sourceTranslit,
                           CultureInfo targetLanguage, string targetText, string targetTranslit)
            : base(
                new TranslationKey(sourceText, sourceLanguage, targetLanguage),
                new TranslationRecord
                    {
                        SourceTransliteration = sourceTranslit,
                        TargetText = targetText,
                        TargetTransliteration = targetTranslit,
                        TranslationTime = DateTime.UtcNow
                    })
        {
        }

        public TranslationKey Key
        {
            get { return Item1; }
        }

        public TranslationRecord Data
        {
            get { return Item2; }
        }

        public CultureInfo SourceLanguage
        {
            get { return IntToCultureInfo(Item1.SourceLanguage); }
        }

        public string SourceText
        {
            get { return Item1.SourceText; }
        }

        public string SourceTransliteration
        {
            get { return Item2.SourceTransliteration; }
        }

        public CultureInfo TargetLanguage
        {
            get { return IntToCultureInfo(Item1.TargetLanguage); }
        }

        public string TargetText
        {
            get { return Item2.TargetText; }
        }

        public string TargetTransliteration
        {
            get { return Item2.TargetTransliteration; }
        }

        public bool EqualsAny(string text)
        {
            return text != null &&
                   (TargetText == text || SourceText == text || SourceTransliteration == text ||
                    TargetTransliteration == text);
        }

        public static TranslationTuple Create(ref TranslationKey key, ref TranslationRecord record)
        {
            return new TranslationTuple(ref key, ref record);
        }

        public override string ToString()
        {
            return string.Format("{0}[{1}] -> {2}", SourceText, SourceTransliteration, TargetText);
        }
    }
}